# RANDOM SELECTION OF ARGUMENTS FOR KIALO VALIDATION CHECK
library(tidyverse)
library(qualtRics)
library(hrbrthemes)
library(modelsummary)

source("scripts/prep_appendix_B1.R")

w1 <- qualtRics::read_survey("data/study2_w1.csv") %>%
 filter(!duplicated(participantId)) %>%
 filter(Progress == 100)

w2 <- qualtRics::read_survey("data/study2_w2.csv") %>%
 filter(!duplicated(participantId)) %>%
 filter(Progress == 100)

study2 <- w1 %>%
 inner_join(w2, by = "participantId")

text_combined <- study2 %>%
 transmute(text = paste(!!!select(., contains("acc")))) %>%
 mutate(text = gsub("NA", "", text)) %>%
 mutate(text = gsub("[^A-Za-z ]","",text)) %>%
 mutate(text = stringr::str_trim(text)) %>%
 pull(text)

prompts <- tibble(name = colnames(study2), # tibble of var names, labels
                  label = labelled::var_label((study2))) %>%
  mutate(name = ifelse(!str_detect(name, "^acc") | str_detect(name, "DO_"),
                       NA, name), # NA if irrelevant
         label = ifelse(!str_detect(name, "^acc") | str_detect(name, "DO_"),
                        NA, label),
         label = ifelse(str_detect(name, "2p2c"), # shorten labels
                        str_c(str_sub(label, -5, -2), str_sub(name, 4, 10)),
                        str_c(str_sub(label, -5, -2), str_sub(name, 4, 6))))

i1 <- match(colnames(study2), prompts$name, nomatch = 0) # find prompt variables
colnames(study2)[i1] <- prompts$label[i1] # replace var names with short labels

response <- function(info, pro, con, mixed){
  if_else(info == "4p", as.character(pro),
          if_else(info == "4c", as.character(con),
                  if_else(info == "2p2c", as.character(mixed), NA_character_)))
}

arguments <- study2 %>%
  transmute(id = 1:nrow(study2), likert.x,
            pro1, pro2, pro3, pro4, con1, con2, con3, con4)

# Randomly draw one participant from each subtopic
set.seed(2664155) # random.org
kialo_sample <- coded_s2 %>%
 left_join(arguments, by = "id") %>%
 filter(generic_flag == 1) %>%
 group_by(subtopic) %>%
 slice_sample(n = 1)



# ANALYSIS OF HUMAN RATINGS FOR ARGUMENT STRENGTH (CloudResearch sample)

# Load list of GPT-generated and Kialo arguments used for the rating task
pairs_df_all <- read.csv("data/kialo_cloudresearch_pairs.csv")

# Load CloudResearch human ratings
kialo_ratings <- qualtRics::read_survey("data/cloudresearch_kialo_ratings.csv") %>%
  filter(!is.na(participantId))

kialo_ratings %>%
  select(id1, id2, id3, id4, id5, id6,
         id7, id8, id9, id10) %>%
  pivot_longer(cols = contains("id"),
               names_to = "trial",
               values_to = "id") %>%
  bind_cols(kialo_ratings %>%
              select(contains("compare_")) %>%
              relocate(compare_10, .after = last_col()) %>%
              pivot_longer(cols = contains("compare"),
                           names_to = "variable",
                           values_to = "rating")) -> long_ratings

long_ratings$participant <- rep(1:200, each = 10)

long_ratings %>% left_join(pairs_df_all %>%
                             mutate(id = row_number())) -> combined_pairs

